//
//  FKFastShareTool.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/17.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKFastShareTool.h"
#import <Photos/PHAssetChangeRequest.h>

static const CGFloat bottomViewHeight = 160.0;

@implementation FKFastShareTool

+ (void)createPhotoWith:(NSString *)imageUrl qrcodeUrl:(NSString *)qrcodeUrl complete:(void (^)(UIImage *))complete
{
//    __block UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW - fk_adjustW(20), fkScreenH - fk_adjustW(80))];
    __block UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, fkScreenH - fk_adjustW(80))];
    container.backgroundColor = [UIColor whiteColor];
    [container fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UIImageView *photoView = [[UIImageView alloc] init];
    [container addSubview:photoView];
    photoView.contentMode = UIViewContentModeScaleAspectFit;
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"new_logo"]];
    [container addSubview:iconView];
    
    UIView *bottomView = [[UIView alloc] init];
    [container addSubview:bottomView];
    
    UIImageView *qrcodeView = [[UIImageView alloc] init];
    [bottomView addSubview:qrcodeView];
    qrcodeView.backgroundColor = fkBgColor;
    
    UILabel *nicknameLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [bottomView addSubview:nicknameLabel];
    
    UILabel *descLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor666666 text:@"顽淘亲子，共度欢乐时光"];
    [bottomView addSubview:descLabel];
    
    UIImageView *tipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"easyBuy"]];
    [bottomView addSubview:tipView];
    
    UIImageView *avatarView = [[UIImageView alloc] init];
    [bottomView addSubview:avatarView];
    [avatarView fk_viewCornerRadius:fk_adjustW(35) borderWidth:0 borderColor:nil];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(100));
    }];
    [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.offset(15);
        make.width.height.offset(fk_adjustW(70));
    }];
    [qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(fk_adjustW(10));
        make.height.width.offset(fk_adjustW(80));
        make.centerY.offset(0);
    }];
    [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(qrcodeView.mas_right).offset(8);
        make.top.equalTo(qrcodeView).offset(4);
    }];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicknameLabel);
        make.bottom.equalTo(qrcodeView);
        make.width.offset(fk_adjustW(130));
        make.height.offset(fk_adjustW(13.5));
    }];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nicknameLabel);
        make.centerY.equalTo(qrcodeView);
    }];
    [avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(70));
        make.centerY.equalTo(qrcodeView);
        make.right.offset(-8);
    }];
    
    __block NSInteger completeCount = 0;
    __block UIImage *snapshotImage;
    [photoView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        // 动态改变container的宽高
//        CGFloat containerRatio = (fkScreenW - fk_adjustW(20)) / (fkScreenH - fk_adjustW(120) - fk_adjustW(100));
        CGFloat containerRatio = (fkScreenW) / (fkScreenH - fk_adjustW(120) - fk_adjustW(100));
        CGFloat imageRatio = image.size.width / image.size.height;
        if (containerRatio < imageRatio) {
            CGFloat containerH = (fkScreenW - fk_adjustW(20)) / imageRatio + fk_adjustW(100);
            container.frame = CGRectMake(0, 0, fkScreenW - fk_adjustW(20), containerH);
        } else {
//            container.frame = CGRectMake(0, 0, fkScreenW - fk_adjustW(20), fkScreenH - fk_adjustW(120));
            container.frame = CGRectMake(0, 0, fkScreenW , fkScreenH - fk_adjustW(120));
        }
        
        
        completeCount++;
        if (completeCount == 2) { // 两张图片都加载完毕
            [container layoutIfNeeded]; // 将子控件渲染到view中去
            snapshotImage = [container fk_snapshotImage];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (complete) complete(snapshotImage);
            });
        }
    }];
    qrcodeView.image = [UIImage fk_imageGenerateQRCode:qrcodeUrl width:200 height:200];
    NSDictionary *userInfo = [FKCacheTool userInfo];
    nicknameLabel.text = userInfo[@"nickname"];
    [avatarView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"avatar"]] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        completeCount++;
        if (completeCount == 2) { // 两张图片都加载完毕
            [container layoutIfNeeded]; // 将子控件渲染到view中去
            snapshotImage = [container fk_snapshotImage];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (complete) complete(snapshotImage);
            });
        }
    }];
}

+ (void)createPhotosWith:(NSArray<NSString *> *)imageUrls qrcodeUrl:(NSString *)qrcodeUrl complete:(void (^)(NSArray<UIImage *> *images))complete
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    __block NSMutableArray *array = [NSMutableArray array];
    for (NSString *url in imageUrls) {
        dispatch_group_enter(group);
        [self createPhotoWith:url qrcodeUrl:qrcodeUrl complete:^(UIImage * _Nonnull image) {
            [array addObject:image];
            dispatch_group_leave(group);
        }];
    }
    // 所有图片生成完毕
    dispatch_group_notify(group, queue, ^{
        // 必须回到主线程，因为block中可能会执行UI相关操作
        dispatch_async(dispatch_get_main_queue(), ^{
            if (complete) complete(array);
        });
    });
}

// 保存素材的方法更变了,增加新的方法
+ (void)createPhotoWith:(NSString *)imageUrl qrcodeUrl:(NSString *)qrcodeUrl saleTime:(NSString *)saleTime activeTime:(NSString *)activeTime level:(NSString *)level index:(NSInteger)index complete:(void (^)(UIImage *))complete {
    if (![level isEqualToString:@"10"]) {
        __block UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, fk_adjustW(10), fkScreenW - fk_adjustW(20), fkScreenH - fk_adjustW(80))];
        container.backgroundColor = [UIColor whiteColor];
        [container fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
        
        UIImageView *photoView = [[UIImageView alloc] init];
        [container addSubview:photoView];
        photoView.contentMode = UIViewContentModeScaleAspectFit;
        
        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"new_logo"]];
        [container addSubview:iconView];
        
        UIView *bottomView = [[UIView alloc] init];
        [container addSubview:bottomView];
        bottomView.layer.masksToBounds = true;
        bottomView.clipsToBounds = true;
        
        UIImageView *qrcodeView = [[UIImageView alloc] init];
        [bottomView addSubview:qrcodeView];
        qrcodeView.backgroundColor = fkBgColor;
        
        UILabel *nicknameLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
        [bottomView addSubview:nicknameLabel];
        
        UILabel *descLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor666666 text:@"我为你推荐"];
        [bottomView addSubview:descLabel];
        
        UIImageView *tipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"easyBuy2"]];
        [bottomView addSubview:tipView];
        
        UIImageView *avatarView = [[UIImageView alloc] init];
        [bottomView addSubview:avatarView];
        [avatarView fk_viewCornerRadius:fk_adjustW(48) borderWidth:0 borderColor:nil];
        // 新增控件
        // bottomView 新增 控件
        UIImageView *iconView2= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
        [bottomView addSubview:iconView2];
        // sale time
        CGSize leadingLabelSize = CGSizeMake(fk_adjustW(57), fk_adjustW(15));
        UILabel *stLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor(255, 255, 255) text:@"抢购时间"];
        stLabel.textAlignment = NSTextAlignmentCenter;
        stLabel.layer.masksToBounds = true;
        stLabel.layer.cornerRadius = 4;
        stLabel.backgroundColor = [UIColor fk_colorWithHexString:@"#DB2C5C"];
        [bottomView addSubview:stLabel];
        UILabel *saleTimeLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor666666];
        [bottomView addSubview:saleTimeLabel];
        // active
        UILabel *atLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor(255, 255, 255) text:@"使用时间"];
        atLabel.textAlignment = NSTextAlignmentCenter;
        atLabel.layer.masksToBounds = true;
        atLabel.layer.cornerRadius = 4;
        atLabel.backgroundColor = [UIColor fk_colorWithHexString:@"#DB2C5C"];
        [bottomView addSubview:atLabel];
        UILabel *activeTimeLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor666666];
        [bottomView addSubview:activeTimeLabel];
        
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.offset(0);
            make.height.offset(fk_adjustW(bottomViewHeight));
        }];
        [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.offset(0);
            make.bottom.equalTo(bottomView.mas_top);
        }];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10);
            make.top.offset(10);
            make.width.height.offset(fk_adjustW(40));
        }];
        [qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(fk_adjustW(18));
            make.height.width.offset(fk_adjustW(90));
            make.bottom.offset(-8);
        }];
        [nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(qrcodeView.mas_right).offset(30);
            make.top.equalTo(qrcodeView).offset(8);
        }];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(bottomView);
            make.bottom.equalTo(bottomView.mas_bottom).offset(-20);
            make.width.offset(fk_adjustW(80));
            make.height.offset(fk_adjustW(35));
        }];
        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(nicknameLabel);
            make.top.equalTo(nicknameLabel.mas_bottom).offset(4);
        }];
        [avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(fk_adjustW(96));
            make.right.offset(-16);
            make.bottom.offset(-6);
        }];
        // 新增的控件
        [iconView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(fk_adjustW(44), fk_adjustW(44)));
            make.right.mas_offset(-10);
            make.top.mas_offset(6);
        }];
        // sale time
        [stLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(leadingLabelSize);
            make.top.mas_offset(10);
            make.left.mas_offset(4);
        }];
        
        saleTime = saleTime.length == 0 ? @"" : saleTime;
        NSMutableAttributedString *saletime = [[NSMutableAttributedString alloc] initWithString:saleTime];
        [saletime addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"EuphemiaUCAS" size:12]} range:NSMakeRange(0, [saleTime length])];
        for (int i = 0; i < [self getRangeStr:saletime.string findText:@"年" ].count; i++) {
            NSInteger location = [[self getRangeStr:saletime.string findText:@"年" ][i] integerValue];
            [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
        }
        for (int i = 0; i < [self getRangeStr:saletime.string findText:@"月" ].count; i++) {
            NSInteger location = [[self getRangeStr:saletime.string findText:@"月" ][i] integerValue];
            [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
        }
        saleTimeLabel.attributedText = saletime;
        [saleTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(stLabel);
            make.top.mas_equalTo(stLabel);
            make.left.mas_equalTo(stLabel.mas_right).offset(10);
            make.right.mas_equalTo(iconView2.mas_left).offset(-10);
        }];
        // active time
        [atLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(leadingLabelSize);
            make.top.mas_equalTo(stLabel.mas_bottom).offset(6);
            make.left.mas_equalTo(stLabel);
        }];
        activeTime = activeTime.length == 0 ? @"" : activeTime;
        NSMutableAttributedString *activetime = [[NSMutableAttributedString alloc] initWithString:activeTime];
        [activetime addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"EuphemiaUCAS" size:12]} range:NSMakeRange(0, [activeTime length])];
        for (int i = 0; i < [self getRangeStr:activetime.string findText:@"年" ].count; i++) {
            NSInteger location = [[self getRangeStr:activetime.string findText:@"年" ][i] integerValue];
            [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
        }
        for (int i = 0; i < [self getRangeStr:activetime.string findText:@"月" ].count; i++) {
            NSInteger location = [[self getRangeStr:activetime.string findText:@"月" ][i] integerValue];
            [activetime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
        }
        activeTimeLabel.attributedText = activetime;
        [activeTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(atLabel);
            make.top.mas_equalTo(atLabel);
            make.left.mas_equalTo(atLabel.mas_right).offset(10);
            make.right.mas_equalTo(iconView2.mas_left).offset(-10);
        }];
        __block NSInteger completeCount = 0;
        __block UIImage *snapshotImage;
        [photoView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            // 动态改变container的宽高
            CGFloat containerRatio = (fkScreenW - fk_adjustW(20)) / (fkScreenH - fk_adjustW(80) - fk_adjustW(bottomViewHeight));
            CGFloat imageRatio = image.size.width / image.size.height;
            if (containerRatio < imageRatio) {
                if (index != 0) {
                    CGFloat containerH = (fkScreenW - fk_adjustW(20)) / imageRatio;
                    container.frame = CGRectMake(0, fk_adjustW(10), fkScreenW - fk_adjustW(20), containerH);
                    [bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.height.offset(0);
                    }];
                } else {
                    CGFloat containerH = (fkScreenW - fk_adjustW(20)) / imageRatio + fk_adjustW(bottomViewHeight);
                    container.frame = CGRectMake(0, fk_adjustW(10), fkScreenW - fk_adjustW(20), containerH);
                }
            } else {
                container.frame = CGRectMake(0, fk_adjustW(10), fkScreenW - fk_adjustW(20), fkScreenH - fk_adjustW(80));
            }
            completeCount++;
            if (completeCount == 2) { // 两张图片都加载完毕
                [container layoutIfNeeded]; // 将子控件渲染到view中去
                snapshotImage = [container fk_snapshotImage];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (complete) complete(snapshotImage);
                });
            }
        }];
        qrcodeView.image = [self addImage:[UIImage imageNamed:@"AppIcon"] toImage:[UIImage fk_imageGenerateQRCode:qrcodeUrl width:200 height:200]];
        NSDictionary *userInfo = [FKCacheTool userInfo];
        NSString *name = [NSString stringWithFormat:@"我是%@",userInfo[@"nickname"]];
        NSMutableAttributedString *nickname = [[NSMutableAttributedString alloc] initWithString:name];
        // red #DB2C5C
        [nickname setAttributes:@{NSFontAttributeName:fkFont12} range:NSMakeRange(0, 2)];
        [nickname setAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#777777"]} range:NSMakeRange(0, 2)];
        [nickname setAttributes:@{NSFontAttributeName:fkFont16} range:NSMakeRange(2, nickname.length - 2)];
        [nickname setAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(2, nickname.length - 2)];
        nicknameLabel.attributedText = nickname;
        [avatarView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"avatar"]] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            completeCount++;
            if (completeCount == 2) { // 两张图片都加载完毕
                [container layoutIfNeeded]; // 将子控件渲染到view中去
                snapshotImage = [container fk_snapshotImage];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (complete) complete(snapshotImage);
                });
            }
        }];
    } else {
        // default View . non vip
        __block UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, fk_adjustW(10), fkScreenW - fk_adjustW(20), fkScreenH - fk_adjustW(80))];
        container.backgroundColor = [UIColor whiteColor];
        [container fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
        
        UIImageView *photoView = [[UIImageView alloc] init];
        [container addSubview:photoView];
        photoView.contentMode = UIViewContentModeScaleAspectFit;
        
        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"new_logo"]];
        [container addSubview:iconView];
        
        UIView *bottomView = [[UIView alloc] init];
        [container addSubview:bottomView];
        bottomView.layer.masksToBounds = true;
        bottomView.clipsToBounds = true;
        
        UIImageView *qrcodeView = [[UIImageView alloc] init];
        [bottomView addSubview:qrcodeView];
        qrcodeView.backgroundColor = fkBgColor;
        
        UIImageView *defaultImg = [[UIImageView alloc] init];
        [defaultImg setImage:[UIImage imageNamed:@"default_bottom_img"]];
//        defaultImg.backgroundColor = UIColor.orangeColor;
        [bottomView addSubview:defaultImg];
        
        // 新增控件
        // bottomView 新增 控件
        UIImageView *iconView2= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
        [bottomView addSubview:iconView2];
        // sale time
        CGSize leadingLabelSize = CGSizeMake(fk_adjustW(57), fk_adjustW(15));
        UILabel *stLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor(255, 255, 255) text:@"抢购时间"];
        stLabel.textAlignment = NSTextAlignmentCenter;
        stLabel.layer.masksToBounds = true;
        stLabel.layer.cornerRadius = 4;
        stLabel.backgroundColor = [UIColor fk_colorWithHexString:@"#DB2C5C"];
        [bottomView addSubview:stLabel];
        UILabel *saleTimeLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor666666];
        [bottomView addSubview:saleTimeLabel];
        // active
        UILabel *atLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor(255, 255, 255) text:@"使用时间"];
        atLabel.textAlignment = NSTextAlignmentCenter;
        atLabel.layer.masksToBounds = true;
        atLabel.layer.cornerRadius = 4;
        atLabel.backgroundColor = [UIColor fk_colorWithHexString:@"#DB2C5C"];
        [bottomView addSubview:atLabel];
        UILabel *activeTimeLabel = [UILabel fk_labelWithFont:fkFont12 textColor:fkColor666666];
        [bottomView addSubview:activeTimeLabel];
        
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.offset(0);
            make.height.offset(fk_adjustW(bottomViewHeight));
        }];
        [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.offset(0);
            make.bottom.equalTo(bottomView.mas_top);
        }];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10);
            make.top.offset(10);
            make.width.height.offset(fk_adjustW(40));
        }];
        [qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(fk_adjustW(18));
            make.height.width.offset(fk_adjustW(90));
            make.bottom.offset(-8);
        }];
        // 新增的控件
        [iconView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(fk_adjustW(44), fk_adjustW(44)));
            make.right.mas_offset(-10);
            make.top.mas_offset(6);
        }];
        // sale time
        [stLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(leadingLabelSize);
            make.top.mas_offset(10);
            make.left.mas_offset(10);
        }];
        NSMutableAttributedString *saletime = [[NSMutableAttributedString alloc] initWithString:saleTime];
        
        [saletime addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"EuphemiaUCAS" size:12]} range:NSMakeRange(0, [saleTime length])];
        for (int i = 0; i < [self getRangeStr:saletime.string findText:@"年" ].count; i++) {
            NSInteger location = [[self getRangeStr:saletime.string findText:@"年" ][i] integerValue];
            [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
        }
        for (int i = 0; i < [self getRangeStr:saletime.string findText:@"月" ].count; i++) {
            NSInteger location = [[self getRangeStr:saletime.string findText:@"月" ][i] integerValue];
            [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
        }
        saleTimeLabel.attributedText = saletime;
        [saleTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(stLabel);
            make.top.mas_equalTo(stLabel);
            make.left.mas_equalTo(stLabel.mas_right).offset(10);
            make.right.mas_equalTo(iconView2.mas_left).offset(-10);
        }];
        // active time
        [atLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(leadingLabelSize);
            make.top.mas_equalTo(stLabel.mas_bottom).offset(6);
            make.left.mas_equalTo(stLabel);
        }];
        NSMutableAttributedString *activetime = [[NSMutableAttributedString alloc] initWithString:activeTime];
        [activetime addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"EuphemiaUCAS" size:12]} range:NSMakeRange(0, [activeTime length])];
        for (int i = 0; i < [self getRangeStr:activetime.string findText:@"年" ].count; i++) {
            NSInteger location = [[self getRangeStr:activetime.string findText:@"年" ][i] integerValue];
            [saletime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
        }
        for (int i = 0; i < [self getRangeStr:activetime.string findText:@"月" ].count; i++) {
            NSInteger location = [[self getRangeStr:activetime.string findText:@"月" ][i] integerValue];
            [activetime addAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(location + 1, 2)];
        }
        activeTimeLabel.attributedText = activetime;

        
        [defaultImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(fk_adjustW(214), fk_adjustW(98)));
            make.bottom.mas_equalTo(-6);
            make.right.mas_equalTo(-24);
        }];
        
        __block NSInteger completeCount = 0;
        __block UIImage *snapshotImage;
        [photoView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            qrcodeView.image = [self addImage:[UIImage imageNamed:@"AppIcon"] toImage:[UIImage fk_imageGenerateQRCode:qrcodeUrl width:200 height:200]];
            // 动态改变container的宽高
            CGFloat containerRatio = (fkScreenW - fk_adjustW(20)) / (fkScreenH - fk_adjustW(80) - fk_adjustW(bottomViewHeight));
            CGFloat imageRatio = image.size.width / image.size.height;
            if (containerRatio < imageRatio) {
                if (index != 0) {
                    CGFloat containerH = (fkScreenW - fk_adjustW(20)) / imageRatio;
                    container.frame = CGRectMake(0, 0, fkScreenW - fk_adjustW(20), containerH);
                    [bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.height.offset(0);
                    }];
                } else {
                    CGFloat containerH = (fkScreenW - fk_adjustW(20)) / imageRatio + fk_adjustW(bottomViewHeight);
                    container.frame = CGRectMake(0, 0, fkScreenW - fk_adjustW(20), containerH);
                }
            } else {
                container.frame = CGRectMake(0, 0, fkScreenW - fk_adjustW(20), fkScreenH - fk_adjustW(80));
            }
            completeCount++;
            if (completeCount == 1) { //
                [container layoutIfNeeded]; // 将子控件渲染到view中去
                snapshotImage = [container fk_snapshotImage];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (complete) complete(snapshotImage);
                });
            }
        }];
    }
}



+ (void)createPhotosWith:(NSArray<NSString *> *)imageUrls qrcodeUrl:(NSString *)qrcodeUrl saleTime:(NSString *)saleTime activeTime:(NSString *)activeTime level:(NSString *)level complete:(void (^)(NSArray<UIImage *> *images))complete {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    __block NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i< imageUrls.count; i++) {
        NSString *url = imageUrls[i];
        dispatch_group_enter(group);
        
        [self createPhotoWith:url qrcodeUrl:qrcodeUrl saleTime:saleTime activeTime:activeTime level:level index:i complete:^(UIImage * _Nonnull image) {
            [array addObject:image];
            dispatch_group_leave(group);
        }];
    }
//    }
    // 所有图片生成完毕
    dispatch_group_notify(group, queue, ^{
        // 必须回到主线程，因为block中可能会执行UI相关操作
        dispatch_async(dispatch_get_main_queue(), ^{
            if (complete) complete(array);
        });
    });
}


/** 保存图片到相册 */
+ (void)saveImages:(NSArray<UIImage *> *)images completion:(void (^)(void))completion
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    for (UIImage *image in images) {
        dispatch_group_enter(group);
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            [PHAssetChangeRequest creationRequestForAssetFromImage:image];
        } completionHandler:^(BOOL success, NSError * _Nullable error) {
            dispatch_group_leave(group);
        }];
    }
    
    dispatch_group_notify(group, queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) completion();
        });
        FKLog(@"全部保存完毕");
    });
}

// qrcode 增加水印
+ (UIImage *)addImage:(UIImage *)firstImage toImage:(UIImage *)secondImage {
    CGFloat firstSizeW = fk_adjustW(30);
    UIGraphicsBeginImageContext(secondImage.size);
    [secondImage drawInRect:CGRectMake(0, 0, secondImage.size.height, secondImage.size.width)];
    [firstImage drawInRect:CGRectMake((secondImage.size.height - firstSizeW)/2, (secondImage.size.width - firstSizeW)/2, firstSizeW, firstSizeW)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

@end
